namespace TuanHA_Combat_Routine
{
    using Styx.TreeSharp;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Threading;

    public class ThrottlePasses : Decorator
    {
        private int _count;
        private DateTime _end;
        private RunStatus _limitStatus;

        public ThrottlePasses(int timeSeconds, Composite child) : this(1, TimeSpan.FromSeconds((double) timeSeconds), (RunStatus) RunStatus.Failure, child)
        {
        }

        public ThrottlePasses(TimeSpan timeFrame, Composite child) : this(1, timeFrame, (RunStatus) RunStatus.Failure, child)
        {
        }

        public ThrottlePasses(int Limit, int timeSeconds, Composite child) : this(Limit, TimeSpan.FromSeconds((double) timeSeconds), (RunStatus) RunStatus.Failure, child)
        {
        }

        public ThrottlePasses(int limit, TimeSpan timeFrame, RunStatus limitStatus, Composite child) : base(child)
        {
            this.TimeFrame = timeFrame;
            this.Limit = limit;
            this._end = DateTime.MinValue;
            this._count = 0;
            this._limitStatus = limitStatus;
        }

        protected override IEnumerable<RunStatus> Execute(object context)
        {
            return new <Execute>d__0(-2) { <>4__this = this, <>3__context = context };
        }

        public override void Start(object context)
        {
            base.Start(context);
        }

        public override void Stop(object context)
        {
            base.Stop(context);
        }

        public int Limit { get; set; }

        public TimeSpan TimeFrame { get; set; }

        [CompilerGenerated]
        private sealed class <Execute>d__0 : IEnumerable<RunStatus>, IEnumerable, IEnumerator<RunStatus>, IEnumerator, IDisposable
        {
            private int <>1__state;
            private RunStatus <>2__current;
            public object <>3__context;
            public ThrottlePasses <>4__this;
            private int <>l__initialThreadId;
            public object context;

            [DebuggerHidden]
            public <Execute>d__0(int <>1__state)
            {
                this.<>1__state = <>1__state;
                this.<>l__initialThreadId = Thread.CurrentThread.ManagedThreadId;
            }

            private bool MoveNext()
            {
                switch (this.<>1__state)
                {
                    case 0:
                        this.<>1__state = -1;
                        if ((DateTime.Now >= this.<>4__this._end) || (this.<>4__this._count < this.<>4__this.Limit))
                        {
                            this.<>4__this.get_DecoratedChild().Start(this.context);
                            while (this.<>4__this.get_DecoratedChild().Tick(this.context) == ((RunStatus) ((int) RunStatus.Running)))
                            {
                                this.<>2__current = (RunStatus) RunStatus.Running;
                                this.<>1__state = 2;
                                return true;
                            Label_00AA:
                                this.<>1__state = -1;
                            }
                            this.<>4__this.get_DecoratedChild().Stop(this.context);
                            if (DateTime.Now > this.<>4__this._end)
                            {
                                this.<>4__this._count = 0;
                                this.<>4__this._end = DateTime.Now + this.<>4__this.TimeFrame;
                            }
                            this.<>4__this._count++;
                            RunStatus? nullable = this.<>4__this.get_DecoratedChild().get_LastStatus();
                            if ((((RunStatus) nullable.GetValueOrDefault()) == RunStatus.Failure) && nullable.HasValue)
                            {
                                this.<>2__current = (RunStatus) RunStatus.Failure;
                                this.<>1__state = 3;
                                return true;
                            }
                            this.<>2__current = (RunStatus) RunStatus.Success;
                            this.<>1__state = 4;
                            return true;
                        }
                        this.<>2__current = this.<>4__this._limitStatus;
                        this.<>1__state = 1;
                        return true;

                    case 1:
                        this.<>1__state = -1;
                        break;

                    case 2:
                        goto Label_00AA;

                    case 3:
                        this.<>1__state = -1;
                        break;

                    case 4:
                        this.<>1__state = -1;
                        break;
                }
                return false;
            }

            [DebuggerHidden]
            IEnumerator<RunStatus> IEnumerable<RunStatus>.GetEnumerator()
            {
                ThrottlePasses.<Execute>d__0 d__;
                if ((Thread.CurrentThread.ManagedThreadId == this.<>l__initialThreadId) && (this.<>1__state == -2))
                {
                    this.<>1__state = 0;
                    d__ = this;
                }
                else
                {
                    d__ = new ThrottlePasses.<Execute>d__0(0) {
                        <>4__this = this.<>4__this
                    };
                }
                d__.context = this.<>3__context;
                return d__;
            }

            [DebuggerHidden]
            IEnumerator IEnumerable.GetEnumerator()
            {
                return this.System.Collections.Generic.IEnumerable<Styx.TreeSharp.RunStatus>.GetEnumerator();
            }

            [DebuggerHidden]
            void IEnumerator.Reset()
            {
                throw new NotSupportedException();
            }

            void IDisposable.Dispose()
            {
            }

            RunStatus IEnumerator<RunStatus>.Current
            {
                [DebuggerHidden]
                get
                {
                    return this.<>2__current;
                }
            }

            object IEnumerator.Current
            {
                [DebuggerHidden]
                get
                {
                    return this.<>2__current;
                }
            }
        }
    }
}

